﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QL_Nha_Sach_Phuong_Nam
{
    public partial class FormEditSach : Form
    {
        private CXulythongtinsach xulyTTS;
        private CXulyNhaxuatban xulyNXB;
        private Cxulytacgia xulyTG;
        private Cxulytheloai xulyTL;
        string strma = "";
        int id;

        public FormEditSach(int button,string ma)
        {
            InitializeComponent();
            if (button == 0)
                btnchung.Text = "Add";
            else
                btnchung.Text = "Save";
            strma = ma;
        }

        private void FormEditSach_Load(object sender, EventArgs e)
        {
            xulyTTS = new CXulythongtinsach();
            xulyNXB = new CXulyNhaxuatban();
            xulyTG = new Cxulytacgia();
            xulyTL = new Cxulytheloai();

            cbbNXB.DataSource = xulyNXB.GetdsNhaXuatBan();
            cbbNXB.DisplayMember = "NhaXuatBanName";
            cbbNXB.ValueMember = "NhaXuatBanNo";

            if (btnchung.Text == "Save")
            {
                Sach sach = xulyTTS.TimSachtheoma(strma);
                if (sach != null && sach.SachStatus == "live")
                {
                    id = sach.SachID;
                    txtten.Text = sach.SachName;
                    txtNamXuatBan.Text = sach.SachNamXuatBan;
                    txtGia.Text = sach.SachPrice.ToString();
                    cbbNXB.Text = sach.NhaXuatBan.NhaXuatBanName;
                    txtMaSach.Text = sach.SachNo;
                    txtdes.Text = sach.SachDescription;
                    foreach (Sach_TacGia stg in sach.Sach_TacGias)
                    {
                        int i = dgvTacGia.Rows.Add();
                        hienthidstacgia(xulyTG.getdsTacgia(), i);
                        dgvTacGia.Rows[i].Cells[0].Value = stg.TacGia.TacGiaNo;
                        dgvTacGia.Rows[i].Cells[1].Value = stg.TacGia.TacGiaName;
                    }
                    foreach (Sach_TheLoai stl in sach.Sach_TheLoais)
                    {
                        int i = dgvTheLoai.Rows.Add();
                        hienthidstheloai(xulyTL.getdsTheLoai(), i);
                        dgvTheLoai.Rows[i].Cells[0].Value = stl.TheLoai.TheLoaiNo;
                        dgvTheLoai.Rows[i].Cells[1].Value = stl.TheLoai.TheLoaiName;
                    }
                }
            }           
        }

        //hien thi ds tac gia len combobox tronng dgv
        private void hienthidstacgia(List<TacGia> tg, int dong)
        {
            DataGridViewComboBoxCell c = (DataGridViewComboBoxCell)dgvTacGia.Rows[dong].Cells[1];
            c.DataSource = tg;
            c.DisplayMember = "TacGiaName";
            c.ValueMember = "TacGiaName";
        }

        //hien thi ds the loai len combobox trong dgv
        private void hienthidstheloai(List<TheLoai> tl, int dong)
        {
            DataGridViewComboBoxCell c = (DataGridViewComboBoxCell)dgvTheLoai.Rows[dong].Cells[1];
            c.DataSource = tl;
            c.DisplayMember = "TheLoaiName";
            c.ValueMember = "TheLoaiName";
        }

        //Truoc khi chon tac gia
        private void dgvTacGia_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            switch (e.ColumnIndex)
            {
                case 1:
                    List<TacGia> ds = xulyTG.getdsTacgia();
                    hienthidstacgia(ds, e.RowIndex);
                    break;
            }
        }

        //truoc khi chon the loai
        private void dgvTheLoai_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            switch (e.ColumnIndex)
            {
                case 1:
                    List<TheLoai> ds = xulyTL.getdsTheLoai();
                    hienthidstheloai(ds, e.RowIndex);
                    break;
            }
        }

        //chon tac gia
        private void dgvTacGia_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            switch (e.ColumnIndex)
            {
                case 1:
                    DataGridViewComboBoxCell c = (DataGridViewComboBoxCell)dgvTacGia.Rows[e.RowIndex].Cells[1];
                    string tentacgia = c.Value.ToString();
                    TacGia tg = xulyTG.TimTacgiatheoten(tentacgia).First();
                    if (tg != null) hienthitacgia(tg, e.RowIndex);
                    break;
            }
        }

        //hien thi tac gia len dgv
        private void hienthitacgia(TacGia tg, int dong)
        {
            dgvTacGia.Rows[dong].Cells[0].Value = tg.TacGiaNo;
        }

        //chon the loai
        private void dgvTheLoai_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            switch (e.ColumnIndex)
            {
                case 1:
                    DataGridViewComboBoxCell c = (DataGridViewComboBoxCell)dgvTheLoai.Rows[e.RowIndex].Cells[1];
                    string tentheloai = c.Value.ToString();
                    TheLoai tl = xulyTL.TimTheloaitheoten(tentheloai);
                    if (tl != null) hienthitheloai(tl, e.RowIndex);
                    break;
            }
        }

        //hien thi the loai len dgv
        private void hienthitheloai(TheLoai tl, int dong)
        {
            dgvTheLoai.Rows[dong].Cells[0].Value = tl.TheLoaiNo;
        }

        private void btnchung_Click(object sender, EventArgs e)
        {
            if (btnchung.Text == "Add")
            {
                Sach sach = new Sach();
                sach.SachID = xulyTTS.getdsSach().Last().SachID + 1;
                sach.SachNo = txtMaSach.Text;
                sach.SachName = txtten.Text;
                sach.NhaXuatBan = xulyNXB.TimNhaXuatBanma(cbbNXB.SelectedValue.ToString());
                sach.SachNamXuatBan = txtNamXuatBan.Text;
                sach.SachPrice = Double.Parse(txtGia.Text);
                sach.SachDescription = txtdes.Text;
                sach.SachStatus = "live";
                foreach (DataGridViewRow r in dgvTacGia.Rows)
                {
                    if (r.IsNewRow) continue;
                    Sach_TacGia stg = new Sach_TacGia();
                    stg.Sach_TacGiaDescription =Convert.ToString(r.Cells[2].Value);
                    string matg = r.Cells[0].Value.ToString();
                    stg.TacGia = xulyTG.TimTacgiatheoma(matg);                 
                    sach.Sach_TacGias.Add(stg);
                }
                foreach (DataGridViewRow r in dgvTheLoai.Rows)
                {
                    if (r.IsNewRow) continue;
                    Sach_TheLoai stl = new Sach_TheLoai();
                    stl.Sach_TheLoaiDescription = Convert.ToString(r.Cells[2].Value);
                    string tl = r.Cells[1].Value.ToString();
                    stl.TheLoai = xulyTL.TimTheloaitheoten(tl);  
                    sach.Sach_TheLoais.Add(stl);
                }

                xulyTTS.ThemSach(sach);
            }
            if (btnchung.Text == "Save")
            {
                Sach editsach = new Sach();
                editsach.SachID = id;
                editsach.SachNo = txtMaSach.Text;
                editsach.SachName = txtten.Text;
                string maNXB = cbbNXB.SelectedValue.ToString();
                editsach.SachNamXuatBan = txtNamXuatBan.Text;
                editsach.SachPrice = Double.Parse(txtGia.Text);
                editsach.SachDescription = txtdes.Text;

                foreach (DataGridViewRow r in dgvTacGia.Rows)
                {
                    if (r.IsNewRow) continue;
                    Sach_TacGia stg = new Sach_TacGia();
                    stg.Sach_TacGiaDescription = Convert.ToString(r.Cells[2].Value);
                    TacGia tg = new TacGia();
                    tg.TacGiaNo = r.Cells[0].Value.ToString();
                    tg.TacGiaName = r.Cells[1].Value.ToString();
                    stg.TacGia = tg;
                    editsach.Sach_TacGias.Add(stg);
                }

                foreach (DataGridViewRow r1 in dgvTheLoai.Rows)
                {
                    if (r1.IsNewRow) continue;
                    Sach_TheLoai stl = new Sach_TheLoai();
                    stl.Sach_TheLoaiDescription = Convert.ToString(r1.Cells[2].Value);
                    TheLoai tl = new TheLoai();
                    tl.TheLoaiNo = r1.Cells[0].Value.ToString();
                    tl.TheLoaiName = r1.Cells[1].Value.ToString();
                    stl.TheLoai = tl;
                    editsach.Sach_TheLoais.Add(stl);
                }

                xulyTTS.SuaSach(editsach, maNXB);
            }
            Close();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            Close();
        }      
    }
}
